package com.lxu.report.util;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 通用工具类
 */
public class CommonUtils {
    /**
     * 下划线转驼峰法(默认小驼峰)
     *
     * @param line
     *            源字符串
     * @param smallCamel
     *            大小驼峰,是否为小驼峰(驼峰，第一个字符是大写还是小写)
     * @return 转换后的字符串
     */
    public static String underline2Camel(String line, boolean ... smallCamel) {
        if (line == null || "".equals(line)) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        Pattern pattern = Pattern.compile("([A-Za-z\\d]+)(_)?");
        Matcher matcher = pattern.matcher(line);
        //匹配正则表达式
        while (matcher.find()) {
            String word = matcher.group();
            //当是true 或则是空的情况
            if((smallCamel.length ==0 || smallCamel[0] ) && matcher.start()==0){
                sb.append(Character.toLowerCase(word.charAt(0)));
            }else{
                sb.append(Character.toUpperCase(word.charAt(0)));
            }

            int index = word.lastIndexOf('_');
            if (index > 0) {
                sb.append(word.substring(1, index).toLowerCase());
            } else {
                sb.append(word.substring(1).toLowerCase());
            }
        }
        return sb.toString();
    }

    /**
     * 格式化为平台可识别的文件路径名称
     * @param absoluteFile
     * @return
     */
    public static String formatFileName(String absoluteFile) {
        if(StringUtils.isBlank(absoluteFile) || !absoluteFile.contains("userfiles")){
            return "不可识别路径";
        }
        String finalName = absoluteFile.substring(absoluteFile.indexOf("userfiles")).replace("\\\\","\\").replace("\\","/");
        StringBuffer sb = new StringBuffer();
        sb.append("[{\"url\":\"/").append(finalName).append("\"}]");
        return sb.toString();
    }

    public static void main(String[] args) {
        String str2 = "[{\"url\":\"/userfiles/1/程序附件//formbuilder/2021/6/01/个体工商户、家庭农牧场类（法人）担保业务初审立项表（农担公司）.docx\"}]";
        String str ="e:\\\\files\\\\/\\userfiles\\\\report_word\\2021\\7\\24\\null_84d8ef50611041a5aa1674914ad2cc82.docx";
        System.out.println(formatFileName(str));
        String finalName = "[{\"url\":\"/userfiles/report_word/2021/7/24/null_84d8ef50611041a5aa1674914ad2cc82.docx\"}]";
        System.out.println(getWordFileName(finalName));
        System.out.println(getFilePath(finalName));
    }

    public static String getFilePath(String fileWord){
        if(StringUtils.isBlank(fileWord) || !fileWord.contains("userfiles")){
            return "不可识别路径";
        }
        return fileWord.substring(fileWord.indexOf("/")+1,fileWord.length()-3);
    }

    public static String getWordFileName(String fileWord) {
        if(StringUtils.isBlank(fileWord) || !fileWord.contains(".doc")){
            return "不可识别文件";
        }
        return  fileWord.substring(fileWord.lastIndexOf("/")+1,fileWord.length()-3);
    }

    public static String getPdfFileName(String filePdf) {
        if(StringUtils.isBlank(filePdf) || !filePdf.contains(".pdf")){
            return "不可识别文件";
        }
        return  filePdf.substring(filePdf.lastIndexOf("/")+1,filePdf.length()-3);
    }
}
